﻿using System;
using System.Diagnostics;
using Castle.DynamicProxy;
using Log.Common;
using Mvvm.Common.Aop;
using WPFCommon.Test.ViewModel;

namespace WPFCommon.Test
{
    /// <summary>
    /// 计算运行时间
    /// <para>bb 2020-05-08 18:17:32</para>
    /// </summary>
    public class CalcRunTimeAttribute : AopBaseAttribute
    {
        [AopPropertiesInject] 
        public ILog Log { get; set; }

        public override Action<IInvocation> Invoke(Action<IInvocation> action)
        {
            return invocation =>
            {
                Stopwatch stop = new Stopwatch();
                stop.Start();
                action.Invoke(invocation);
                stop.Stop();
                var str = $"方法：{invocation.Method.Name}，运行时间：{stop.Elapsed.TotalSeconds}s";
                Trace.WriteLine(str);
                Log.Info(str);
            };
        }
    }
}
